var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    inputValue: '',
    sfhidden:'0',
    bjval:40

  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => { },
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  preventTouchMove: function () {

  },
  changeFocus: function () {
    console.log('aaaaa')
    var that = this;
    that.setData({
      focus: true
    })
  },

  go: function () {
    this.setData({
      focus: true
    })
  },
   /**
   * 获取聚焦
   */
  focus: function(e) {
    var that = this
    
    let wxWin = wx.getSystemInfoSync();
    let wxDom=wx.createSelectorQuery();
    //let that=this;
    wxDom.selectAll('.chatbottom,.scroll-view').boundingClientRect(function (res) {
      let talkHeight = wxWin.windowHeight - e.detail.height;
      console.log(that.data.curMesg.length*60,res[0].height,talkHeight)
      var curHeight=e.detail.height+300
      that.setData({
        'talkHeight': talkHeight,
        'talkPaddingBottom':0,
        'bjval':curHeight
      });
    }).exec()

  },
  bindblur(e){
    var that = this
    var curHeight=40
    that.setData({
      'bjval':curHeight
    });  
  },
  toAgree: function (e) {

    var that = this;
    var msgid = e.currentTarget.dataset.tagid
    var attendeeid = that.data.attendeeid
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/message/msgprocessing'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["msgid"] = msgid
      k["status"] = 2
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'msgid' + msgid + 'status2' + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          that.setData({
            inputValue: ''
          })
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var code = mesg.code
          var message = mesg.message
          //var code = curdata.code
          console.log('code=' + code)
          if (code == 200) {
            if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
              && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
              var timestamp = Date.parse(new Date());
              timestamp = timestamp / 1000;
              var cururl = app.globalData.picoaddress + '/attendee/message/history'
              var m = {}
              m["v"] = '1.0.0'
              m["client"] = 'ios'
              m["timestamp"] = timestamp
              m["lan"] = 'cn'
              m["exhibitionid"] = exhibitionid
              m["page"] = 1
              m["attendeeid"] = attendeeid
              //联系人id
              m["u"] = u
              var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'page1' + 'timestamp' + timestamp + app.globalData.picotoken
              console.log(sign)
              var curb64 = md5.zhMD5(sign)
              console.log(curb64)
              m["sign"] = curb64
              wx.request({
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                },
                url: cururl,
                method: 'post',
                data: m,
                success(res) {
                  console.log(res)
                  var mesg = res.data
                  console.log('mesg=' + mesg)
                  var curdata = mesg.data.reverse() // 倒置
                  console.log('curdataleg='+curdata.length)
                  var curdataleg = curdata.length
                  //软键盘是否覆盖信息
                  var sfhidden = that.data.sfhidden
                  if(curdataleg<=5){
                    sfhidden='1'
                  }else{
                    sfhidden='0'
                  }
                  that.setData({
                    'curMesg': curdata,
                    'sfhidden': sfhidden
                  });

                }, fail: res => {
                  console.log(res, "error")
                }
              })

            }
          } else {
            wx.showToast({
              title: '抱歉，暂不能发送信息！',
              icon: 'none',
              duration: 2000
            })
          }
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  toRefuse: function (e) {
    var that = this;
    var msgid = e.currentTarget.dataset.tagid
    var toid = that.data.attendeeid
    var attendeeid = that.data.attendeeid
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/message/msgprocessing'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["msgid"] = msgid
      k["status"] = -1
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'msgid' + msgid + 'status-1' + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          that.setData({
            inputValue: ''
          })
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var code = mesg.code
          var message = mesg.message
          //var code = curdata.code
          console.log('code=' + code)
          if (code == 200) {
            if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
              && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
              var timestamp = Date.parse(new Date());
              timestamp = timestamp / 1000;
              var cururl = app.globalData.picoaddress + '/attendee/message/history'
              var m = {}
              m["v"] = '1.0.0'
              m["client"] = 'ios'
              m["timestamp"] = timestamp
              m["lan"] = 'cn'
              m["exhibitionid"] = exhibitionid
              m["page"] = 1
              m["attendeeid"] = attendeeid
              //联系人id
              m["u"] = u
              var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'page1' + 'timestamp' + timestamp + app.globalData.picotoken
              console.log(sign)
              var curb64 = md5.zhMD5(sign)
              console.log(curb64)
              m["sign"] = curb64
              wx.request({
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                },
                url: cururl,
                method: 'post',
                data: m,
                success(res) {
                  console.log(res)
                  var mesg = res.data
                  console.log('mesg=' + mesg)
                  var curdata = mesg.data.reverse() // 倒置
                  console.log('curdataleg='+curdata.length)
                  var curdataleg = curdata.length
                  //软键盘是否覆盖信息
                  var sfhidden = that.data.sfhidden
                  if(curdataleg<=5){
                    sfhidden='1'
                  }else{
                    sfhidden='0'
                  }
                  that.setData({
                    'curMesg': curdata,
                    'sfhidden': sfhidden
                  });

                }, fail: res => {
                  console.log(res, "error")
                }
              })

            }
          } else {
            wx.showToast({
              title: '抱歉，暂不能发送信息！',
              icon: 'none',
              duration: 2000
            })
          }
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  sendMesg: function (e) {
    var mesg = e.detail.value
    var that = this;
    var toid = that.data.attendeeid
    var attendeeid = that.data.attendeeid
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/message/send'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["msgtype"] = '0'
      k["toid"] = toid
      k["msg"] = mesg
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'msg' + mesg + 'msgtype0' + 'timestamp' + timestamp + 'toid' + toid + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          that.setData({
            inputValue: ''
          })
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var code = mesg.code
          var message = mesg.message
          //var code = curdata.code
          console.log('code=' + code)
          if (code == 200) {
            if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
              && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
              var timestamp = Date.parse(new Date());
              timestamp = timestamp / 1000;
              var cururl = app.globalData.picoaddress + '/attendee/message/history'
              var m = {}
              m["v"] = '1.0.0'
              m["client"] = 'ios'
              m["timestamp"] = timestamp
              m["lan"] = 'cn'
              m["exhibitionid"] = exhibitionid
              m["page"] = 1
              m["attendeeid"] = attendeeid
              //联系人id
              m["u"] = u
              var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'page1' + 'timestamp' + timestamp + app.globalData.picotoken
              console.log(sign)
              var curb64 = md5.zhMD5(sign)
              console.log(curb64)
              m["sign"] = curb64
              wx.request({
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                },
                url: cururl,
                method: 'post',
                data: m,
                success(res) {
                  console.log(res)
                  var mesg = res.data
                  console.log('mesg=' + mesg)
                  var curdata = mesg.data.reverse() // 倒置
                  console.log('curdataleg='+curdata.length)
                  var curdataleg = curdata.length
                  //软键盘是否覆盖信息
                  var sfhidden = that.data.sfhidden
                  if(curdataleg<=5){
                    sfhidden='1'
                    
                  }else{
                    sfhidden='0'
                    that.setData({
                      focus: true,
                      oldfocus:false
                    })
                  }
                  that.setData({
                    'curMesg': curdata,
                    'sfhidden': sfhidden
                  });
                  wx.createSelectorQuery().select('#notificdMain').boundingClientRect(function (rect) {
                    wx.pageScrollTo({
                      scrollTop: rect.bottom,
                      duration: 0
                    })
                  }).exec();

                }, fail: res => {
                  console.log(res, "error")
                }
              })

            }
          } else {
            wx.showToast({
              title: '抱歉，暂不能发送信息！',
              icon: 'none',
              duration: 2000
            })
          }
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数 
    var attendeeid = options.attendeeid
    console.log('attendeeid=', attendeeid)
    that.setData({
      attendeeid: attendeeid,
      focus: false
    })
    var nickname = options.nickname
    wx.setNavigationBarTitle({
      title: nickname
    })
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    //exhibitionid = '33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var cururl = app.globalData.picoaddress + '/attendee/message/history'
      var m = {}
      m["v"] = '1.0.0'
      m["client"] = 'ios'
      m["timestamp"] = timestamp
      m["lan"] = 'cn'
      m["exhibitionid"] = exhibitionid
      m["page"] = 1
      m["attendeeid"] = attendeeid
      //联系人id
      m["u"] = u
      var sign = app.globalData.picotoken + 'attendeeid' + attendeeid + 'clientiosexhibitionid' + exhibitionid + 'page1' + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      m["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: cururl,
        method: 'post',
        data: m,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data.reverse() // 倒置
          console.log('curdataleg='+curdata.length)
          var curdataleg = curdata.length
          //软键盘是否覆盖信息
          var sfhidden = that.data.sfhidden
          if(curdataleg<=5){
            sfhidden='1'
            that.setData({
              focus: false
            })
          }else{
            sfhidden='0'
           
          }
          that.setData({
            'curMesg': curdata,
            'sfhidden': sfhidden
          });
          wx.createSelectorQuery().select('#notificdMain').boundingClientRect(function (rect) {
            wx.pageScrollTo({
              scrollTop: rect.bottom,
              duration: 0
            })
          }).exec();
        }, fail: res => {
          console.log(res, "error")
        }
      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  toDetail(e) {
    console.log(e)
    var attendeeid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../pageDetail/pageDetails?attendeeid=' + attendeeid,
    })
  },
})